﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Shuriken.Base
{
    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class DefaultActionAttribute : Attribute { }

    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class ActionNameAttribute : Attribute
    {
        protected string m_actionName;

        public ActionNameAttribute(string actionName)
        {
            m_actionName = actionName;
        }

        /// <summary>
        /// Name of the action 
        /// </summary>
        public string ActionName
        {
            get
            {
                return m_actionName;
            }
        }
    }

    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class ActionIconAttribute : Attribute
    {
        protected string m_actionIcon;

        public ActionIconAttribute(string actionIcon)
        {
            m_actionIcon = actionIcon;
        }

        /// <summary>
        /// Name of the action 
        /// </summary>
        public string ActionIcon
        {
            get
            {
                return m_actionIcon;
            }
        }
    }

    /// <summary>
    /// Attribute used for decorating the action with its subject type
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class ActionSubjectTypeAttribute : Attribute
    {
        protected Type m_subject;

        public ActionSubjectTypeAttribute(Type subject)
        {
            m_subject = subject;
        }

        /// <summary>
        /// Subject type for this action
        /// </summary>
        public Type ActionSubject
        {
            get
            {
                return m_subject;
            }
        }
    }

    [AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
    public class AttributedActionAttribute : Attribute { }

    /// <summary>
    /// Provides an abstract class to help implement actions
    /// </summary>
    public abstract class BaseAction : IAction
    {
        /// <summary>
        /// Retrieves the ActionName attribute value 
        /// </summary>
        /// <returns>name of the action</returns>
        protected virtual string RetrieveActionNameAttribute()
        {
            ActionNameAttribute attrActionName = (ActionNameAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(ActionNameAttribute));

            if (null == attrActionName)
            {
                throw new Exception("Name attribute is not defined for this action!");
            }
            else
            {
                return attrActionName.ActionName;
            }
        }

        /// <summary>
        /// Retrieves the ActionIcon attribute value 
        /// </summary>
        /// <returns>name of the action</returns>
        protected virtual string RetrieveActionIconAttribute()
        {
            ActionIconAttribute attrActionIcon = (ActionIconAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(ActionIconAttribute));

            if (null == attrActionIcon)
            {
                return string.Empty;
            }
            else
            {
                return attrActionIcon.ActionIcon;
            }
        }

        /// <summary>
        /// Retrieves the type of the subject that this action acts on. 
        /// </summary>
        /// <returns>type of subject</returns>
        protected virtual Type RetrieveSubjectTypeAttribute()
        {
            ActionSubjectTypeAttribute attrSubjectType = (ActionSubjectTypeAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(ActionSubjectTypeAttribute));

            if (null == attrSubjectType)
            {
                throw new Exception("Subject type is not defined for this action!");
            }
            else
            {
                return attrSubjectType.ActionSubject;
            }
        }

        /// <summary>
        /// Retrieves the value of DefaultActionAttribute 
        /// </summary>
        /// <returns>true, if this action is default for its subject</returns>
        protected virtual bool RetrieveDefaultAttribute()
        {
            DefaultActionAttribute attrDefaultAction = (DefaultActionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(DefaultActionAttribute));

            if (null == attrDefaultAction)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Retrieves the value of AttributedActionAttribute 
        /// </summary>
        /// <returns>true, if this action is default for its subject</returns>
        protected virtual bool RetrieveAttributedAttribute()
        {
            AttributedActionAttribute attrDefaultAction = (AttributedActionAttribute)Attribute.GetCustomAttribute(this.GetType(), typeof(AttributedActionAttribute));

            if (null == attrDefaultAction)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        #region IAction Members

        public abstract void Execute(ISubject subject);

        public abstract bool CanExecuteOn(ISubject subject);

        /// <summary>
        /// Subject type for this action 
        /// </summary>
        public Type SubjectType
        {
            get { return RetrieveSubjectTypeAttribute(); }
        }

        /// <summary>
        /// Default Action 
        /// </summary>
        public bool IsDefault
        {
            get { return RetrieveDefaultAttribute(); }
        }

        /// <summary>
        /// Action attribute
        /// </summary>
        public string ActionAttribute
        {
            get
            {
                return string.Empty;
            }
            set
            {
            }
        }

        /// <summary>
        /// Action can have attribute.
        /// </summary>
        public bool IsAttributed
        {
            get { return RetrieveAttributedAttribute(); }
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return RetrieveActionNameAttribute(); }
        }

        public string Description
        {
            get { return string.Empty; }
        }

        public string Icon
        {
            get
            {
                return RetrieveActionIconAttribute();
            }
            set
            {
      
            }
        }

        #endregion
    }
}
